package com.j2205.hongzhiyuan.controller;

import com.alibaba.fastjson.JSON;
import com.j2205.hongzhiyuan.pojo.ChangCiInfo;
import com.j2205.hongzhiyuan.service.ChangCiService;
import com.j2205.hongzhiyuan.service.impl.ChangCiServiceImpl;
import com.j2205.hongzhiyuan.vo.VoChangCi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ChangCiServlet extends BaseServlet{
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ParseException, IOException {
        String id = req.getParameter("cgID");
        int cgID = Integer.parseInt(id);
        String openDate1 = req.getParameter("input0");
        Date openDate=new SimpleDateFormat("yyyy-MM-dd").parse(openDate1);
        int  maxNumber = Integer.parseInt(req.getParameter("txt_num")==""?"200":req.getParameter("txt_num"));
        int maxOrderNumber=Integer.parseInt(req.getParameter("txtOderNumber")==""?"200":req.getParameter("txtOrderNumber"));
        String startTime0=req.getParameter("TextBox3");
        Date startTime=new SimpleDateFormat("HH:mm:ss").parse(startTime0);
        String endTime0 = req.getParameter("TextBoxclose");
        Date endTime = new SimpleDateFormat("HH:mm:ss").parse(endTime0);
        String status =(req.getParameter("status"));
        if (status.equals("0")){
            status="未审核";
        }else{
            status="已审核";

        }

        ChangCiInfo info=new ChangCiInfo();
        info.setCgID(cgID);
        info.setEndTime(endTime);
        info.setMaxNumber(maxNumber);
        info.setStartTime(startTime);
        info.setMaxOrderNumber(maxOrderNumber);
        info.setOpenDate(openDate);
        info.setStatus(status);
        System.out.println(info);
        ChangCiService changciService = new ChangCiServiceImpl();
        boolean b = changciService.update(info);
        System.out.println(b);
        if(b){
            resp.sendRedirect("sys/Manage.html");
        }
    }
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ParseException, IOException {
        String openDate1 = req.getParameter("input0");
        Date openDate=new SimpleDateFormat("yyyy-MM-dd").parse(openDate1);
        int  maxNumber = Integer.parseInt(req.getParameter("txt_num")==""?"200":req.getParameter("txt_num"));
        int maxOrderNumber=Integer.parseInt(req.getParameter("txtOderNumber")==""?"200":req.getParameter("txtOrderNumber"));
        String startTime0=req.getParameter("TextBox3");
        Date startTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime0);
        String endTime0 = req.getParameter("TextBoxclose");
        Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime0);
        ChangCiInfo info=new ChangCiInfo();
        info.setEndTime(endTime);
        info.setMaxNumber(maxNumber);
        info.setStartTime(startTime);
        info.setMaxOrderNumber(maxOrderNumber);
        info.setOpenDate(openDate);
        info.setStatus("未审核");
        ChangCiService changciService = new ChangCiServiceImpl();
        boolean b = changciService.addChangci(info);
        if(b){
            resp.sendRedirect("sys/Manage.html");
        }
    }
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        System.out.println(" jinlai");
        ChangCiService changCiService=new ChangCiServiceImpl();
        List<VoChangCi> infos=changCiService.findAll();
        String json = JSON.toJSONString(infos);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        System.out.println("json = " + json);

        writer.println(json);
        writer.flush();
        writer.close();
    }
    protected void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String openDate  = req.getParameter("openDate");
        String status = req.getParameter("status");
        ChangCiService changciService = new ChangCiServiceImpl();
        List<VoChangCi> vcs=changciService.select(openDate,status);
        String json = JSON.toJSONString(vcs);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println(json);
        writer.flush();
        writer.close();
        System.out.println("json = " + json);
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id=Integer.parseInt(req.getParameter("id"));
        System.out.println(id);
        ChangCiService service=new ChangCiServiceImpl();
        boolean b=service.deleteById(id);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if(b){
            writer.write("success");
        }else{
            writer.write("fail");
        }

        writer.flush();
        writer.close();

    }
    protected void audit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id=Integer.parseInt(req.getParameter("id"));
        System.out.println(id);
        ChangCiService service=new ChangCiServiceImpl();
        VoChangCi voChangCi=service.findById(id);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if(voChangCi.getStatus().equals("未审核")||voChangCi.getStatus().isEmpty()){
            service.auditById(id);
            writer.write("success");
        }else{
            writer.write("fail");
        }

        writer.flush();
        writer.close();

    }
    protected void selectById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id=Integer.parseInt(req.getParameter("id"));
        System.out.println(id);
        ChangCiService service=new ChangCiServiceImpl();
        VoChangCi vcs=service.findById(id);
        System.out.println(vcs);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        String json = JSON.toJSONString(vcs);
        writer.write(json);
        writer.flush();
        writer.close();

    }
    public void plshenHe(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String cgIDs = req.getParameter("cgIDs");
        String [] ids = cgIDs.split(",");
        List<Integer> ids1 = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            int i1 = Integer.parseInt(ids[i]);
            ids1.add(i1);
        }
        System.out.println(ids1);
        ChangCiService changciInfoService = new ChangCiServiceImpl();
        boolean b = changciInfoService.plshenHe(ids1);
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (b){
            writer.write("success");
        }else {
            writer.write("fail");
        }
        writer.flush();
        writer.close();
    }
}
